package cn.com.twoke.application.game.mario.core.ecs.component.render.animation;


import cn.com.twoke.application.game.mario.core.ecs.component.render.RenderComponent;
import cn.com.twoke.application.game.mario.core.ecs.component.render.RenderHandler;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 动画渲染组件
 */
@Getter
@Setter
@Builder(toBuilder = true)
@RenderHandler(AnimationRenderComponentHandler.class)
public class AnimationRenderComponent implements RenderComponent {
    /**
     * 纹理图单帧的范围（x,y,width,height）
     */
    private final Rectangle bounds;
    // 序列帧的长度
    private final int animationTickCount;
    /**
     * 当前动画运行的序列帧索引
     */
    @Builder.Default
    private int animationTick = 0;
    /**
     * 当前动画帧运行的帧数时间
     */
    @Builder.Default
    private int animationCostTime = 0;
    /**
     * 动画帧运行最大帧数时间获取回调，默认每帧最大运行帧数时间 5（帧数） * 16.67 (每帧时间) = 83.35 每帧动画运行5帧，运行83.35毫秒
     */
    @Builder.Default
    private final Function<Integer, Integer> animationTickMaxTimeFun = idx -> 5;
    /**
     * 动画帧数图获取回调
     */
    private final Function<Integer, BufferedImage> animationTickTextureFun;
    @Builder.Default
    public Integer index = 0;
    @Builder.Default
    private Supplier<Boolean> isShow = () -> Boolean.TRUE;

    @Override
    public Boolean isDisplay() {
        return isShow.get();
    }

    public synchronized void incAnimationTick() {
        this.animationTick++;
    }
    public synchronized void incAnimationCostTime() {
        this.animationCostTime ++;
    }

    public Integer getAnimationTickMaxTime() {
        return animationTickMaxTimeFun.apply(this.animationTick);
    }

    public BufferedImage getAnimationTickTexture() {
        return animationTickTextureFun.apply(this.animationTick);
    }
}
